<template>
  <div>
    <el-table :data="registrationList" style="width: 100%; margin-top: 20px;">
      <el-table-column prop="id" label="ID" width="60"/>
      <el-table-column prop="lectureTitle" label="讲堂标题"/>
      <el-table-column prop="userName" label="用户"/>
      <el-table-column prop="status" label="状态"/>
      <el-table-column prop="registrationTime" label="报名时间"/>
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button size="small" @click="editRegistration(scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteRegistration(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="showEdit" title="编辑报名">
      <el-form :model="form">
        <el-form-item label="状态"><el-input v-model="form.status"/></el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showEdit = false">取消</el-button>
        <el-button type="primary" @click="saveRegistration">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { getRegistrationListApi, updateRegistrationApi, deleteRegistrationApi } from '../api/lectureRegistration'
const registrationList = ref([])
const showEdit = ref(false)
const form = ref({})
const loadRegistrations = async () => { registrationList.value = await getRegistrationListApi() }
const editRegistration = (row) => { form.value = { ...row }; showEdit.value = true }
const saveRegistration = async () => {
  await updateRegistrationApi(form.value)
  showEdit.value = false
  loadRegistrations()
}
const deleteRegistration = async (id) => { await deleteRegistrationApi(id); loadRegistrations() }
onMounted(loadRegistrations)
</script> 